package com.aiti.lulian.service.impl;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.constant.MessageCodeConstant;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CustomerTagGroupDto;
import com.aiti.lulian.entity.CustomerTag;
import com.aiti.lulian.entity.CustomerTagGroup;
import com.aiti.lulian.feign.CloudShowManagerSubService;
import com.aiti.lulian.mapper.CustomerTagGroupMapper;
import com.aiti.lulian.service.ICustomerTagGroupService;
import com.aiti.lulian.service.ICustomerTagService;
import com.aiti.lulian.vo.CustomerTagGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CustomerTagGroupServiceImpl extends BaseSerivceImpl<CustomerTagGroupMapper, CustomerTagGroup> implements ICustomerTagGroupService {

    private ICustomerTagService customerTagService;

    private CloudShowManagerSubService cloudShowManagerSubService;

    public CustomerTagGroupServiceImpl(ICustomerTagService customerTagService, CloudShowManagerSubService cloudShowManagerSubService) {
        this.customerTagService = customerTagService;
        this.cloudShowManagerSubService = cloudShowManagerSubService;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Message addOrUpdateTagGroup(CustomerTagGroupDto customerTagGroupDto) {
        CustomerTagGroup customerTagGroup = new CustomerTagGroup();
        BeanUtils.copyProperties(customerTagGroupDto, customerTagGroup);
        if (StrUtil.isBlank(customerTagGroupDto.getBaseId())) {
            //新增标签组
            customerTagGroup.setCreator(customerTagGroupDto.getUserId());
            customerTagGroup.setBaseCreateTime(new Date());
        } else {
            //编辑标签组
            customerTagGroup.setUpdateBy(customerTagGroupDto.getUserId());
            customerTagGroup.setBaseUpdateTime(new Date());
//            //清空组下原有标签
//            customerTagService.remove(new LambdaQueryWrapper<CustomerTag>().eq(CustomerTag::getTagGroupId, customerTagGroupDto.getBaseId()));
        }
        boolean saveOrUpdate = this.saveOrUpdate(customerTagGroup);

//        //保存组下标签
//        List<CustomerTag> tags = customerTagGroupDto.getTags();
//        List<CustomerTag> tagList = tags.stream().peek(i -> {
//            i.setTagGroupId(customerTagGroup.getBaseId());
//            i.setEnterpriseId(customerTagGroup.getEnterpriseId());
//            i.setCreator(customerTagGroupDto.getUserId());
//            i.setBaseCreateTime(new Date());
//        }).collect(Collectors.toList());
//        boolean saveBatch = customerTagService.saveBatch(tagList);

        if (saveOrUpdate) {
            return Message.success("操作成功");
        } else {
            return Message.fail("操作失败");
        }
    }

    @Override
    public Message deleteTagGroup(CustomerTagGroupDto customerTagGroupDto) {
        List<CustomerTag> tagList = customerTagService.list(new LambdaQueryWrapper<CustomerTag>().eq(CustomerTag::getTagGroupId, customerTagGroupDto.getBaseId()));
        if (tagList.size() > 0) {
            return Message.fail("该分组下已存在标签，请先清空标签");
        }
        boolean remove = this.removeById(customerTagGroupDto.getBaseId());
        if (remove) {
            return Message.success("删除成功");
        } else {
            return Message.fail("删除失败");
        }
    }

    @Override
    public Message getTagGroupList(CustomerTagGroupDto customerTagGroupDto) {
        //获取当前用户的权限
        String scope;
        String userId = customerTagGroupDto.getUserId();
        Message userAccessScopeMessage = cloudShowManagerSubService.queryUserAccessScope(userId);
        if (userAccessScopeMessage == null) {
            throw new RuntimeException("获取用户访问权限失败");
        } else if (! userAccessScopeMessage.getCode().equals(MessageCodeConstant.MSG_SUCCESS_CODE)) {
            throw new RuntimeException("查询用户访问权限失败");
        } else {
            scope = (String) userAccessScopeMessage.getObj();
            List<CustomerTagGroup> tagGroupList = this.list(new LambdaQueryWrapper<CustomerTagGroup>().eq(CustomerTagGroup::getEnterpriseId, customerTagGroupDto.getEnterpriseId()).orderByAsc(CustomerTagGroup::getBaseCreateTime));
            List<CustomerTagGroupVo> tagGroupVoList = tagGroupList.stream().map(i -> {
                CustomerTagGroupVo customerTagGroupVo = new CustomerTagGroupVo();
                BeanUtils.copyProperties(i, customerTagGroupVo);
                List<CustomerTag> tagList = customerTagService.getList(scope, userId, i.getBaseId());
                customerTagGroupVo.setTags(tagList);
                return customerTagGroupVo;
            }).collect(Collectors.toList());
            return Message.success(tagGroupVoList);
        }
    }

    @Override
    public Message getTagGroupDetails(String baseId) {
        CustomerTagGroup tagGroup = this.getById(baseId);
        return Message.success(tagGroup);
    }
}